<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use app\BaseController;
use think\facade\View;
use app\middleware\CheckLogin;
use app\model\SettingCar as CarModel;

class Car extends BaseController
{
    // 注册控制器中间件
    protected $middleware = [CheckLogin::class];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //获取所有数据
        $car = CarModel::select();

        View::assign('car',$car);

        return view('index'); 
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
         //
        return view('create'); 
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = request()->param();
        $result = CarModel::create($data);//调用create方法写入数据库
        if($result == false){
            return 0;
        }else{
            return $result->id;
        }
    }

    /**
     * 返回车型配置json.
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function carConfigJson(Request $request)
    {
        //获取车系数据
        $vehicle = $request->param('vehicle');

        //获取车型配置数据
        $config = CarModel::where('vehicle',$vehicle)->where('valid','1')->Distinct(true)->column('config');

        //返回
        return json($config);
    }

    public function carInfoJson(Request $request)
    {
        //获取车系数据
        $vehicle['vehicle'] = $request->param('vehicle');

        //获取配置数据
        $vehicle['config'] = $request->param('config');

        //获取车型配置数据
        $data = CarModel::where($vehicle)->find();

        $info = array();
        if(!empty($data)){
            //分离颜色
            $color = explode(',',$data['color']);

            //分离内饰
            $trim = explode(',',$data['trim']);
            
            //组装数组
            $info['color'] = $color;
            $info['trim'] = $trim;
            $info['price'] = $data['price'];
            $info['model'] = $data['model'];
        }
        //返回
        return json($info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //获取此前数据
        $data = CarModel::where('id',$id)->find();
        View::assign('data',$data);

        return view('edit'); 
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = request()->param();
        $result = CarModel::update($data);//调用create方法写入数据库
        if($result == false){
            return 0;
        }else{
            return $result->id;
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //删除
        $car = CarModel::find($id);//删除id号
        $result = $car->delete();//确认删除结果，返回布尔值
        if($result){
            return "ok";//成功则返回OK
        }else{
            return "false";//失败则返回false
        }
    }
}
